package cn.monkey.transport.netty;

import cn.monkey.transport.core.ExchangeContext;
import io.netty.util.AttributeKey;
import io.netty.util.AttributeMap;

public class AttributeKeyContext implements ExchangeContext {
    private final AttributeMap attributeMap;
    public AttributeKeyContext(AttributeMap attributeMap) {
        this.attributeMap = attributeMap;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T setAttribute(String key, T val) {
        return (T) this.attributeMap.attr(AttributeKey.valueOf(key)).getAndSet(val);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getAttribute(String key) {
        return (T) this.attributeMap.attr(AttributeKey.valueOf(key)).get();
    }
}
